package com.velocity.controller;

import com.velocity.service.SysGeneratorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lijingyang
 * @Title:
 * @Package
 * @Description:
 * @date 2021/7/28 19:14
 */
@Api(value = "生成代码")
@Controller
@RequestMapping("/generator")
public class SysGeneratorController {
    @Autowired
    private SysGeneratorService sysGeneratorService;

    @RequestMapping(value = "/getTableList",method = RequestMethod.GET)
    @ResponseBody
    public List queryTableList() {
        Map<String, Object> map = new HashMap();
        List<Map<String, Object>> list = sysGeneratorService.queryList(map);
        return list;
    }

    /**
     * 生成代码
     */
    @ApiOperation("生成代码")
    @PostMapping(value = "/code")
    public void code(@RequestBody String[] tableNames, HttpServletResponse response) throws IOException {

        byte[] data = sysGeneratorService.generatorCode(tableNames);

        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"x-springboot.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IOUtils.write(data, response.getOutputStream());
    }
}